package cn.lll.vuehouse.controller;


import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import cn.lll.vuehouse.common.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("captcha")
public class CaptchaController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @GetMapping
    public ResponseResult getCaptcha(){
        ResponseResult responseResult = new ResponseResult();

        LineCaptcha lineCaptcha = new LineCaptcha(110,50,4,3);
        String code = lineCaptcha.getCode();
        System.out.println("code = " + code);
        //将验证码图片转换为base64字符串
        String imageBase64 = lineCaptcha.getImageBase64();
        System.out.println("imageBase64 = " + imageBase64);
        //缓存在redis中 同时设置时效性    uuid
        String uuid = IdUtil.simpleUUID();
        System.out.println("uuid = " + uuid);
        stringRedisTemplate.opsForValue().set(uuid,code,3, TimeUnit.MINUTES);


        HashMap<String , String> map = new HashMap<>();
        map.put("imguuid",uuid);
        map.put("img",imageBase64);
        responseResult.setCode(200);
        responseResult.setData(map);
        return responseResult;
    }

}
